interface MyChessman {
  chessName: string;
  camp: string;
  x: number;
  y: number;
  key: string;
  isChecked: boolean;
  gameOver: Function;
  getWinner: Function | null;
}

type predictionMove = {
  path: Array<Array<number>>;
  data?: Chessman;
};

class Chessman implements MyChessman {
  chessName: string;
  camp: string;
  x: number;
  y: number;
  key: string;
  chessId: number;
  isChecked: boolean = false;
  getWinner: Function | null;
  static chessNameList = [
    "ChessCar",
    "ChessHorse",
    "ChessElephant",
    "ChessGuards",
    "ChessKing",
    "ChessCannon",
    "ChessSoldier",
  ];
  constructor(chessName, camp, x, y, key, chessId) {
    this.chessName = chessName;
    this.camp = camp;
    this.x = x;
    this.y = y;
    this.key = key;
    this.chessId = chessId;
  }

  setGetWinner(fn) {
    this.getWinner = fn;
  }

  predictionMove(chessList, path?): any {}

  move(x, y, chessman, chessList) {
    let canMove = true;
    const prevPath = [this.x, this.y];
    let win = false;
    const king = chessList.get(
      this.camp === "red" ? "5GZvKH5MDd" : "1RRev9T3z1"
    );
    const myKing =
      chessman.chessId === 4
        ? chessman
        : chessList.get(this.camp === "red" ? "1RRev9T3z1" : "5GZvKH5MDd");

    this.x = x;
    this.y = y;
    // 检查移动后自己是否被将军
    myKing.checkKiller(chessList, false, chessman);
    canMove = !myKing.killer.size;
    win = king.checkKiller(chessList, true);
    if (win) {
      console.log("绝杀！你赢了");
      // alert("绝杀！你赢了");
      this.getWinner(this.camp);
      return true;
    }
    if (!canMove) {
      this.x = prevPath[0];
      this.y = prevPath[1];
      console.log("移动后被将军");
      return canMove;
    }
    return canMove;
  }

  checked(checked) {
    this.isChecked = checked;
  }

  gameOver(winner?: Function) {
    console.log(winner);
    return winner;
  }

  // 平移棋子 车、炮
  translate(temp, chessList, isCar = false) {
    const pathResolve = {
      startX: 0,
      startY: 0,
      endX: 8,
      endY: 9,
    };
    for (let i = 0; i < 9; i++) {
      temp.push([i, this.y]);
    }
    for (let i = 0; i < 10; i++) {
      temp.push([this.x, i]);
    }
    const borderChesses = [null, null, null, null];
    chessList.forEach((item) => {
      if (item.x === this.x) {
        // 判断直线边界有无棋子
        if (item.y === 0) {
          borderChesses[2] = true;
        } else if (item.y === 9) {
          borderChesses[3] = true;
        }
        if (item.y > this.y && item.y < pathResolve.endY) {
          pathResolve.endY = item.y;
        } else if (item.y < this.y && item.y > pathResolve.startY) {
          pathResolve.startY = item.y;
        }
      } else if (item.y === this.y) {
        // 判断直线边界有无棋子
        if (item.x === 0) {
          borderChesses[0] = true;
        } else if (item.x === 8) {
          borderChesses[1] = true;
        }
        if (item.x > this.x && item.x < pathResolve.endX) {
          pathResolve.endX = item.x;
        } else if (item.x < this.x && item.x > pathResolve.startX) {
          pathResolve.startX = item.x;
        }
      }
    });
    temp.forEach((item, i) => {
      const [x, y] = item;
      if (x > pathResolve.endX || x < pathResolve.startX) temp[i] = null;
      if (y > pathResolve.endY || y < pathResolve.startY) temp[i] = null;
      if (y === this.y && x === this.x) temp[i] = null;
      if (!isCar) {
        if (
          (this.y === y &&
            ((x === pathResolve.endX &&
              (borderChesses[1] || pathResolve.endX !== 8)) ||
              (x === pathResolve.startX &&
                (borderChesses[0] || pathResolve.startX !== 0)))) ||
          (this.x === x &&
            ((y === pathResolve.endY &&
              (borderChesses[3] || pathResolve.endY !== 9)) ||
              (y === pathResolve.startY &&
                (borderChesses[2] || pathResolve.startY !== 0))))
        ) {
          temp[i] = null;
        }
        // if (this.y === y) {
        //   // 左右
        // borderChesses[2] || pathResolve.startX !== 0 判断边界是否为结束点并且有棋子
        // } else if (this.x === x) {
        //   // 上下
        // }
      }
    });
    return pathResolve;
  }

  filterChessman(temp, chessList) {
    return temp.filter((item) => {
      return (
        item !== null &&
        !chessList.some((chessman) => {
          // 判断落脚点是否存在己方棋子
          return (
            chessman[1].camp === this.camp &&
            chessman[1].x === item[0] &&
            chessman[1].y === item[1]
          );
        })
      );
    });
  }
}
export { MyChessman, Chessman };
export default Chessman;
